package com.studychen.appfxcp;

import com.studychen.appfxcp.commom.tools.MoveUtils;
import com.studychen.appfxcp.commom.model.MainViewModel;
import com.studychen.appfxcp.commom.view.MainView;
import de.saxsys.mvvmfx.FluentViewLoader;
import de.saxsys.mvvmfx.MvvmFX;
import de.saxsys.mvvmfx.ViewTuple;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.extern.slf4j.Slf4j;

/**
 * @author :chenping
 * @date :Created in 2023/6/27 14:02
 */
@Slf4j
public class AppApplication extends Application {

    private Stage stage;

    @Override
    public void init() throws Exception {
        log.info("应用初始化...");
    }


    @Override
    public void start(Stage stage) {
        ViewTuple<MainView, MainViewModel> viewTuple = FluentViewLoader.fxmlView(MainView.class).load();
        Parent root = viewTuple.getView();
//        root.setStyle("-fx-background-color: lightblue;");
        root.setStyle("-fx-background-color: rgba(92.0,184.0,117.0,0.7)");
        // 透明背景色的重要操作
//        Scene scene = new Scene(root, Color.TRANSPARENT);
        Scene scene = new Scene(root);
        // 添加样式文件
        scene.getStylesheets().addAll("/com/studychen/appfxcp/commom/css/index.css");
        stage.setScene(scene);
        stage.setTitle("appfx-cp");
        stage.initStyle(StageStyle.TRANSPARENT);
        stage.getIcons().add(new Image("/com/studychen/appfxcp/commom/img/appfx-ap.png"));
        MainView mainView = viewTuple.getCodeBehind();
        // 使顶部可以拖动
        MoveUtils.setMoveControl(mainView.getTitleContent(), stage);
        // 各种消息的绑定
        messageDeal();
        this.stage = stage;
        stage.show();
    }

    @Override
    public void stop() throws Exception {
    }

    private void messageDeal(){
        // 退出事件
        MvvmFX.getNotificationCenter().subscribe("exit", (key, payload) -> {
            // trigger some actions
            Platform.runLater(() -> {
                stage.close();
            });

        });
    }
}
